﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Services.Exceptions;
    using DotNetNuke.Services.Scheduling;
    using Microsoft.VisualBasic;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;

    public class CreateSalePrices : SchedulerClient
    {
        public CreateSalePrices(ScheduleHistoryItem objScheduleHistoryItem)
        {
            this.ScheduleHistoryItem = objScheduleHistoryItem;
        }

        private void DoSalePrices()
        {
            IEnumerator enumerator=null;
            PortalController controller = new PortalController();
            PromoController controller2 = new PromoController();
            ArrayList allPortals = new SettingsController().GetAllPortals();
            try
            {
                enumerator = allPortals.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    PortalInfo current = (PortalInfo) enumerator.Current;
                    if ((SharedFunctions.GetStoreSetting(current.PortalID, "version", "None") != "") && (SharedFunctions.GetStoreSetting(current.PortalID, "salerates.lastday", "None") != DateAndTime.Day(DateAndTime.Today).ToString()))
                    {
                        controller2.createSalePriceTable(current.PortalID, -1);
                        SharedFunctions.SetStoreSetting(current.PortalID, "salerates.lastday", DateAndTime.Day(DateAndTime.Today).ToString(), "None", true);
                    }
                }
            }
            finally
            {
                if (enumerator is IDisposable)
                {
                    (enumerator as IDisposable).Dispose();
                }
            }
        }

        public override void DoWork()
        {
            try
            {
                this.Progressing();
                this.DoSalePrices();
                this.ScheduleHistoryItem.Succeeded = true;
                this.ScheduleHistoryItem.AddLogNote("Sale Prices Calculation Completed.");
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception objException = exception1;
                this.ScheduleHistoryItem.Succeeded = false;
                this.ScheduleHistoryItem.AddLogNote("Sale Prices Calculation  failed." + objException.ToString());
                this.ScheduleHistoryItem.Succeeded = false;
                this.Errored(ref objException);
                DotNetNuke.Services.Exceptions.Exceptions.LogException(objException);
                ProjectData.ClearProjectError();
            }
        }
    }
}

